package cn.yh.nft.turbo.order.domain.entity.statemachine;

import cn.yh.nft.turbo.api.order.constant.TradeOrderEvent;
import cn.yh.nft.turbo.api.order.constant.TradeOrderState;
import cn.yh.nft.turbo.base.statemachine.BaseStateMachine;

/**
 *  订单状态机转换类
 * @ClassName: OrderStateMachine
 * @Author: 草莓爱上啵啵
 */
public class OrderStateMachine extends BaseStateMachine<TradeOrderState, TradeOrderEvent> {

    public static final OrderStateMachine INSTANCE = new OrderStateMachine();

    {
        //创建订单转换到确认订单，只有创建订单状态可以转换成确认订单状态
        putTransition(TradeOrderState.CREATE, TradeOrderEvent.CONFIRM, TradeOrderState.CONFIRM);
        //确认状态转换到支付状态，只有支付成功才能转换
        putTransition(TradeOrderState.CONFIRM, TradeOrderEvent.PAY, TradeOrderState.PAID);
        //库存预扣减成功，但是未真正扣减成功， 也能支付/取消，不能因为延迟导致用户无法支付/取消
        putTransition(TradeOrderState.CREATE, TradeOrderEvent.PAY, TradeOrderState.PAID);
        //创建订单转换到取消订单，只有创建订单状态可以转换成取消订单状态
        putTransition(TradeOrderState.CREATE, TradeOrderEvent.CANCEL, TradeOrderState.CLOSED);
        //创建订单转换到超时订单，只有创建订单状态可以转换
        putTransition(TradeOrderState.CREATE, TradeOrderEvent.TIME_OUT, TradeOrderState.CLOSED);

        //已支付，再确认，状态不变
        putTransition(TradeOrderState.PAID, TradeOrderEvent.CONFIRM, TradeOrderState.PAID);

        //已确认，再取消，状态不变
        putTransition(TradeOrderState.CONFIRM, TradeOrderEvent.CANCEL, TradeOrderState.CLOSED);
        //已确认，再超时，状态不变
        putTransition(TradeOrderState.CONFIRM, TradeOrderEvent.TIME_OUT, TradeOrderState.CLOSED);

        //已支付，再完成，状态不变
        putTransition(TradeOrderState.PAID, TradeOrderEvent.FINISH, TradeOrderState.FINISH);

    }
}
